package com.sws.worktive.wechat;

import android.content.Context;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;

public class WeChatHandler {
	private final String wE_CHAT_APP_ID="wx64a431ab90afe1d8";
	
	private IWXAPI wechatAPI;
	
	private Context mContext;
	
	public WeChatHandler(Context context){
		mContext = context;
	}
	
	public boolean registerToWeChat(){
		wechatAPI = WXAPIFactory.createWXAPI(mContext, wE_CHAT_APP_ID, true);
		return wechatAPI.registerApp(wE_CHAT_APP_ID);
	}
	
	public boolean sendToWX(String messageString){
		
		WXTextObject textObj = new WXTextObject();
		textObj.text = messageString;

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = textObj;
		msg.description = messageString;

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("text");
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneTimeline;
		
		return wechatAPI.sendReq(req);

	}
	
	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
	
}
